<template>
    <div class="tech-order-review-page">
        <!-- 地址展示、选择 -->
        <ReviewAddress :warehouseInfo="orderReviewList[0].warehouseInfo" />
        <!-- 订单列表 -->
        <ReviewOrderItem v-for="(item, index) in orderReviewList" :key="index" :itemInfo="item" />
        <!-- 会员信息 -->
        <ReviewMember
            v-if="memberCardInfo"
            :cardInfo="memberCardInfo"
            :discountPrice="cardDiscountInfo ? cardDiscountInfo.off : 0"
            :totalPrice="totalPrice"
            :isUseMemberPay="isLowerBalance ? false : isCardPay"
            :isCanChange="!isLowerBalance"
            @changePayType="changePayType"
        />
        <ReviewActionBar :price="totalPrice / 100" :discount="totalDiscountPrice / 100" @pay="handleOrderCreate" />
    </div>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import ReviewAddress from './review/ReviewAddress';
import ReviewOrderItem from './review/ReviewOrderItem';
import ReviewMember from './review/ReviewMember';
import ReviewActionBar from './review/ReviewActionBar';
import { Tip, TipLoading } from '@common/tip';
import { getReviewProdList } from '@biztools/review';

import { PageMixin } from '@common/mixin';
import logger from '@common/logger';
export default {
    name: 'OrderReview', // 订单确认页
    mixins: [PageMixin],
    components: {
        ReviewAddress,
        ReviewOrderItem,
        ReviewMember,
        ReviewActionBar,
    },
    computed: {
        ...mapState({
            orderRequestId: state => state.review.orderRequestId,
            orderReviewList: state => state.review.orderReviewList,
            deliveryType: state => state.review.deliveryType,
            addressInfo: state => state.review.addressInfo,
            memberCardInfo: state => state.review.memberCardInfo,
            totalPrice: state => state.review.totalPrice,
            totalDiscountPrice: state => state.review.totalDiscountPrice,
            isCardPay: state => state.review.isCardPay,
            cardDiscountInfo: state => state.review.cardDiscountInfo,
        }),
        // 是否是余额不足
        isLowerBalance() {
            return this.memberCardInfo.balance < this.totalPrice;
        },
    },
    data() {
        return {
            // totalPrice: 0,
            // totalDiscount: 0,
        };
    },
    async mounted() {
        await this.getOrderRequestId();
        await this.getMemberCardInfo();
        await this.getOrderReviewCoupon();
        await this.dealOrderMarket();
        if (this.addressInfo) {
            await this.matchReviewListWarehouseInfo();
        }
    },
    methods: {
        ...mapActions([
            'getOrderRequestId',
            'getMemberCardInfo',
            'getOrderReviewCoupon',
            'calcOrderPrice',
            'dealOrderMarket',
            'matchReviewListWarehouseInfo',
            'changeCardPayFlag',
            'createCombinedOrder',
            'payCombinedOrder',
            'deleteOnlineShopBagData',
        ]),
        // 切换支付方式
        changePayType(isCardPay) {
            this.changeCardPayFlag(isCardPay);
        },
        checkParams() {
            if (!this.addressInfo) {
                Tip('请先选择地址');
                return false;
            }
            return true;
        },
        // 下单
        async handleOrderCreate() {
            TipLoading('确认订单中...');
            if (this.checkParams()) {
                const flag = await this.createCombinedOrder();
                if (flag) {
                    const list = getReviewProdList(this.orderReviewList);
                    logger.warn('Review => 删除线上的数据', list);
                    // 清除线上购物车数据
                    await this.deleteOnlineShopBagData(list);
                    // 执行支付
                    await this.payCombinedOrder();
                }
            }
        },
    },
};
</script>

<style lang="less">
.tech-order-review-page {
    .m-b(100);
}
</style>
